# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2020/9/13 15:15
desc: 模型页面page object
'''

import logging
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from base.pypom import Page
from base.ui_func import readiness_of_document

class ModelListPage(Page):
    URL_TEMPLATE = '/#/model/list'


    _search_model_id = (By.ID, 'searchModelId') # 模型Id搜索框
    _search_model_name= (By.ID, "searchModelName")  # 模型名称输入框
    _search_model_type = (By.ID, 'searchModelType')  # 模型类型搜索框

    _button_search = (By.CSS_SELECTOR,'button.el-button > span:nth-child(2)') # 搜索按钮

    _count_of_models = (By.CSS_SELECTOR,'span.el-pagination__total') # 搜索结果总计数
    _active_page_num = (By.CSS_SELECTOR,'li.number.active')  # active页码
    _link_add_model = (By.CSS_SELECTOR,"div.empty_box > p > a") #搜索结果为空时的添加模型按钮


    _button_add_model = (By.ID, "AddModelBtn")  # 添加模型按钮
    _radio_model_type_selected = (By.CSS_SELECTOR, "label[aria-checked=true]") # Todo 模型类型单选框, 待更正
    _input_model_name = (By.ID, "addModelName")  # 模型名称输入框
    _button_add_model_cancel = (By.ID, "addModelCancelBtn") # 添加模型对话框的取消按钮

    search_mapping={"model_id":_search_model_id, "model_name":_search_model_name,"model_type":_search_model_type}

    def __init__(self,driver,base_url):
        super().__init__(driver,base_url)
        self.get(self.URL_TEMPLATE)
        logging.info("model page loading")
        # IMPORTANT: 等待页面加载完成
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self._active_page_num))

        #WebDriverWait(self.driver, 10).until(readiness_of_document())

    # def loaded(self):
    #     pass

    def input_model_id(self,id):
        self.input(self._search_model_id, id)

    def click_search_button(self):
        self.click(*self._button_search)


    def get_count_of_models(self):
        text = self.driver.find_element(*self._count_of_models).text
        count = int(text.lstrip('共').rstrip('条').strip(' '))
        return count


    def click_add_model_button(self):
        self.click(*self._button_add_model)

    def search_model(self,by,text):
        if by in ['model_id','model_name','model_type']:
            selector = self.search_mapping[by]
            self.input(selector,text)

        self.click_search_button()











